﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Parser.RazorParser
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;

namespace System.Web.Razor.Parser
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Represents a Razor parser.</summary>
  public class RazorParser
  {
    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Parser.RazorParser" /> class.</summary>
    /// <param name="codeParser">The code parser.</param>
    /// <param name="markupParser">The markup parser.</param>
    public RazorParser(ParserBase codeParser, ParserBase markupParser)
    {
      if (codeParser == null)
        throw new ArgumentNullException(nameof (codeParser));
      this.MarkupParser = markupParser != null ? markupParser : throw new ArgumentNullException(nameof (markupParser));
      this.CodeParser = codeParser;
      this.Optimizers = (IList<ISyntaxTreeRewriter>) new List<ISyntaxTreeRewriter>()
      {
        (ISyntaxTreeRewriter) new WhiteSpaceRewriter(new Action<SpanBuilder, SourceLocation, string>(this.MarkupParser.BuildSpan)),
        (ISyntaxTreeRewriter) new ConditionalAttributeCollapser(new Action<SpanBuilder, SourceLocation, string>(this.MarkupParser.BuildSpan))
      };
    }

    internal ParserBase CodeParser { get; private set; }

    internal ParserBase MarkupParser { get; private set; }

    internal IList<ISyntaxTreeRewriter> Optimizers { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets the design time mode.</summary>
    /// <returns>The design time mode.</returns>
    public bool DesignTimeMode { get; set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Parses the specified object.</summary>
    /// <param name="input">The object to parse.</param>
    /// <param name="visitor">The visitor.</param>
    public virtual void Parse(TextReader input, ParserVisitor visitor)
    {
      ParserResults core = this.ParseCore((ITextDocument) new SeekableTextReader(input));
      visitor.Visit(core);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Parses the specified object.</summary>
    /// <returns>The parser result.</returns>
    /// <param name="input">The object to parse.</param>
    public virtual ParserResults Parse(TextReader input) => this.ParseCore((ITextDocument) new SeekableTextReader(input));

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Parses the specified object.</summary>
    /// <returns>The parser result.</returns>
    /// <param name="input">The object to parse.</param>
    public virtual ParserResults Parse(ITextDocument input) => this.ParseCore(input);

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Parses the specified object.</summary>
    /// <param name="input">The object to parse.</param>
    /// <param name="visitor">The visitor.</param>
    [Obsolete("Lookahead-based readers have been deprecated, use overrides which accept a TextReader or ITextDocument instead")]
    public virtual void Parse(LookaheadTextReader input, ParserVisitor visitor)
    {
      ParserResults core = this.ParseCore((ITextDocument) new SeekableTextReader((TextReader) input));
      visitor.Visit(core);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Parses the specified object.</summary>
    /// <returns>The parser result.</returns>
    /// <param name="input">The object to parse.</param>
    [Obsolete("Lookahead-based readers have been deprecated, use overrides which accept a TextReader or ITextDocument instead")]
    public virtual ParserResults Parse(LookaheadTextReader input) => this.ParseCore((ITextDocument) new SeekableTextReader((TextReader) input));

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Creates a task that parses a specified object.</summary>
    /// <returns>The created <see cref="T:System.Threading.Tasks.Task" />.</returns>
    /// <param name="input">The object to parse.</param>
    /// <param name="spanCallback">The span callback.</param>
    /// <param name="errorCallback">The error callback.</param>
    public virtual Task CreateParseTask(
      TextReader input,
      Action<Span> spanCallback,
      Action<RazorError> errorCallback)
    {
      return this.CreateParseTask(input, (ParserVisitor) new CallbackVisitor(spanCallback, errorCallback));
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Creates a task that parses a specified object.</summary>
    /// <returns>The created <see cref="T:System.Threading.Tasks.Task" />.</returns>
    /// <param name="input">The object to parse.</param>
    /// <param name="spanCallback">The span callback.</param>
    /// <param name="errorCallback">The error callback.</param>
    /// <param name="context">The context.</param>
    public virtual Task CreateParseTask(
      TextReader input,
      Action<Span> spanCallback,
      Action<RazorError> errorCallback,
      SynchronizationContext context)
    {
      return this.CreateParseTask(input, (ParserVisitor) new CallbackVisitor(spanCallback, errorCallback)
      {
        SynchronizationContext = context
      });
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Creates a task that parses a specified object.</summary>
    /// <returns>The created <see cref="T:System.Threading.Tasks.Task" />.</returns>
    /// <param name="input">The object to parse.</param>
    /// <param name="spanCallback">The span callback.</param>
    /// <param name="errorCallback">The error callback.</param>
    /// <param name="cancelToken">The cancellation token.</param>
    public virtual Task CreateParseTask(
      TextReader input,
      Action<Span> spanCallback,
      Action<RazorError> errorCallback,
      CancellationToken cancelToken)
    {
      TextReader input1 = input;
      CallbackVisitor callbackVisitor = new CallbackVisitor(spanCallback, errorCallback);
      callbackVisitor.CancelToken = new CancellationToken?(cancelToken);
      return this.CreateParseTask(input1, (ParserVisitor) callbackVisitor);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Creates a task that parses a specified object.</summary>
    /// <returns>The created <see cref="T:System.Threading.Tasks.Task" />.</returns>
    /// <param name="input">The object to parse.</param>
    /// <param name="spanCallback">The span callback.</param>
    /// <param name="errorCallback">The error callback.</param>
    /// <param name="context">The context.</param>
    /// <param name="cancelToken">The cancellation token.</param>
    public virtual Task CreateParseTask(
      TextReader input,
      Action<Span> spanCallback,
      Action<RazorError> errorCallback,
      SynchronizationContext context,
      CancellationToken cancelToken)
    {
      TextReader input1 = input;
      CallbackVisitor callbackVisitor = new CallbackVisitor(spanCallback, errorCallback);
      callbackVisitor.SynchronizationContext = context;
      callbackVisitor.CancelToken = new CancellationToken?(cancelToken);
      return this.CreateParseTask(input1, (ParserVisitor) callbackVisitor);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Creates a task that parses a specified object.</summary>
    /// <returns>The created <see cref="T:System.Threading.Tasks.Task" />.</returns>
    /// <param name="input">The object to parse.</param>
    /// <param name="consumer">The consumer.</param>
    public virtual Task CreateParseTask(TextReader input, ParserVisitor consumer) => new Task((Action) (() =>
    {
      try
      {
        this.Parse(input, consumer);
      }
      catch (OperationCanceledException ex)
      {
      }
    }));

    private ParserResults ParseCore(ITextDocument input)
    {
      ParserContext parserContext = new ParserContext(input, this.CodeParser, this.MarkupParser, this.MarkupParser)
      {
        DesignTimeMode = this.DesignTimeMode
      };
      this.MarkupParser.Context = parserContext;
      this.CodeParser.Context = parserContext;
      this.MarkupParser.ParseDocument();
      ParserResults parserResults = parserContext.CompleteParse();
      System.Web.Razor.Parser.SyntaxTree.Block block = parserResults.Document;
      foreach (ISyntaxTreeRewriter optimizer in (IEnumerable<ISyntaxTreeRewriter>) this.Optimizers)
        block = optimizer.Rewrite(block);
      Span span1 = (Span) null;
      foreach (Span span2 in block.Flatten())
      {
        span2.Previous = span1;
        if (span1 != null)
          span1.Next = span2;
        span1 = span2;
      }
      return new ParserResults(block, parserResults.ParserErrors);
    }
  }
}
